package com.example.order_product.Controller;


import com.example.order_product.Entity.Order;
import com.example.order_product.Entity.Product;
import com.example.order_product.Service.ProductService;
import jakarta.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/product")
public class ProductController {

    private final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }


    // http://localhost:8080/product/addProduct
    @PostMapping("addProduct")
    public ResponseEntity<?> addProduct(@Valid @RequestBody Product product,
                                        BindingResult result) {
        if (result.hasErrors()) {
            // 收集所有的错误信息
            List<String> errorMessages = result.getAllErrors().stream()
                    .map(error -> error.getDefaultMessage())

                    .collect(Collectors.toList());
            // 返回包含所有错误信息的响应
            return ResponseEntity.badRequest().body(errorMessages);
        }
        Product productExist = productService.findProductByName(product.getName());
        if (productExist != null) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", "要添加的产品已经存在");
            return ResponseEntity
                    .badRequest()
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(response);
        }
        productService.addProduct(product);
        return ResponseEntity.ok(product);
    }

    @DeleteMapping("/deleteProduct")
    public ResponseEntity<?> deleteProduct(String name) {
        Product productExist = productService.findProductByName(name);
        if (productExist == null) return ResponseEntity.notFound().build();
        List<Order> orderList = productService.findOrderByProductId(productExist.getId());
        if (orderList.size() > 0) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", "还有订单未完成，不能删除此产品");
            return ResponseEntity
                    .badRequest()
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(response);
        }
        productService.deleteProduct(productExist.getId());
        return ResponseEntity.ok(productExist);
    }

    @PutMapping("/updateProduct")
    public ResponseEntity<?> updateProduct(@Valid @RequestBody Product product,
                                           BindingResult result) {
        if (result.hasErrors()) {
            // 收集所有的错误信息
            List<String> errorMessages = result.getAllErrors().stream()
                    .map(error -> error.getDefaultMessage())
                    .collect(Collectors.toList());
            // 返回包含所有错误信息的响应
            return ResponseEntity.badRequest().body(errorMessages);
        }
        Product productExist = productService.findProductByName(product.getName());
        if (productExist == null) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", "该产品不存在");
            return ResponseEntity
                    .badRequest()
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(response);
        }
        productService.updateProduct(product);
        return ResponseEntity.ok(product);
    }

    @GetMapping("/findProductByName")
    public ResponseEntity<?> findProductByName(String name) {
        Product productExist = productService.findProductByName(name);
        if (productExist == null) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", "该产品不存在");
            return ResponseEntity
                    .badRequest()
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(response);
        }
        return ResponseEntity.ok(productExist);
    }

    @GetMapping("/getAllProduct")
    public ResponseEntity<List<Product>> getAllProduct() {
        List<Product> products = productService.getAllProduct();
        return ResponseEntity.ok(products);
    }

}
